package backpage.validator;

import backpage.annotation.EmployExist;
import backpage.entity.Employee;
import backpage.service.Impl.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


/**
 * EmployExist具体校验器
 *
 * @author 刘仁杰
 */
@Component
public class EmployExistValidator implements ConstraintValidator<EmployExist, Integer> {


    @Autowired
    private EmployeeServiceImpl employeeService;

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext constraintValidatorContext) {
        if (value == null) {
            return false;
        }


        Employee employee = employeeService.queryEmpInfoById(value);
        if (employee == null) {
            constraintValidatorContext.disableDefaultConstraintViolation();
            constraintValidatorContext.buildConstraintViolationWithTemplate("员工不存在").addConstraintViolation();
            return false;
        }
        return true;
    }

}
